<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class CoreUser Extends CorePerson {
    private $mUsername;
    private $mSalt;
    private $mLevel;
    private $mPlainPassword;
    private $mValiditas;
    private $mStatus;

    public function __construct($pPersonId = null) {
        //USER ID=PERSON ID
        if ($pPersonId != null) {
            $this->mPersonId = $pPersonId;
            $this->GetDetail();
        }
    }

    public function GenerateSalt() {
        $tSalt = rand(0, 1028);
        return $tSalt;
    }

    public function GetListOtorisasi() {
        $mQuery = new DBQueryParse();
        $tResult = array();

        $tSQL = "SELECT id_level as id, nama_level as value FROM app_user_level ORDER BY id_level";
        $tRes = $mQuery->Execute($tSQL);

        if ($tRes->Count() > 0) {
            return $tRes;
        } else {
            return false;
        }
    }

    public function GenerateStatusUser() {
        $mQuery = new DBQueryParse();

        $tSQL_status = 'SELECT id_status as id, nama_status as value FROM app_user_status ORDER BY id_status';
        $tResStatus = $mQuery->Execute($tSQL_status);
        $i = 0;
        while ($tResStatus->MoveNext()) {
            $tOutput = $tResStatus->GetRecord($i);
            $tStatus[$i]['Caption'] = $tOutput[1];
            switch ($tOutput[0]) {
                case 0 : $tStatus[$i]['Class'] = "GetSet";
                    break;
                case 1 : $tStatus[$i]['Class'] = "Active";
                    break;
                case 2 : $tStatus[$i]['Class'] = "Non Aktif";
                    break;
            }
            $i++;
        }

        return $tStatus;
    }

    /* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== */

    //MUTUATOR
    public function SetPlainPassword($pPassword) {
        $this->mPlainPassword = $pPassword;
    }

    public function SetUserId($pPersonId) {
        $this->mPersonId = $pPersonId;
    }

    public function SetUsername($pUsername) {
        $this->mUsername = $pUsername;
    }

    public function SetLevel($pLevel) {
        $this->mLevel = $pLevel;
    }

    public function SetSalt($pSalt) {
        $this->mSalt = $pSalt;
    }

    public function GetSalt() {
        $mData = new DBQueryParse();
        $tSQL = 'SELECT salt FROM app_user WHERE person_id= \'' . $this->mPersonId . '\' ';
        //echo $tSQL;
        $tRes = $mData->Execute($tSQL);
        if ($tRes->MoveFirst())
            return $tRes->salt;
        else
            return false;
    }
    
    public function GetUserId() {
        return $this->mPersonId;
    }
    
    public function GetUsername() {
        return $this->mUsername;
    }
    
    public function GetLevel() {
        return $this->mLevel;
    }
    
    public function GetLevelNama() {
        return $this->mLevelNama;
    }
    
    public function GetValiditas() {
        return $this->mValiditas;
    }
    
    public function GetStatus() {
        return $this->mStatus;
    }

    private function GetDetail() {
        $tResult = array();
        $mQueryMhs = new DBQueryExecute();

        $tSQL = "SELECT nama_lengkap, username, salt, level, level_id, validitas, status FROM view_app_user WHERE id_person=\"" . $this->mPersonId . "\" "; //echo 
        $tRes = $mQueryMhs->Execute($tSQL);

        if ($tRes->Count() > 0) {
            $tRes->MoveFirst();
            $this->mUsername = $tRes->username;
            $this->mSalt = $tRes->salt;
            $this->mLevel = $tRes->level_id;
            $this->mLevelNama = $tRes->level;
            $this->mValiditas = $tRes->validitas;
            $this->mStatus = $tRes->status;
            
            $this->GetDetailPerson();
        }
        else
            return false;
    }

}

?>
